<template>
  <div class="work-calendar">
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" placeholder="选择年" style="width: 100px" size="samll" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>

      <el-select
        v-model="currentMonth"
        placeholder="选择月"
        style="width: 100px; margin-left: 10px"
        size="samll"
        @change="dateChange"
      >
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <!-- date 单元格代表的日期  data 该日期的信息 -->
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
<script>
export default {
  name: 'WorkCalendar',
  filters: {
    getDay(value) {
      const day = value.split('-')[2]
      return day.startsWith('0') ? day.substr(1) : day
    }
  },
  props: {
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      yearList: [], // 年的列表  前5年和后5年
      currentYear: null, // 当前年
      currentMonth: null, // 当前月
      currentDate1: null, // 当前日
      currentDate: null // 当前日期
    }
  },
  created() {
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentDate1 = this.startDate.getDate()
    this.yearList = Array.from(Array(11), (value, index) => this.currentYear + index - 5) // 生成一个前五年和后五年的数组
    this.dateChange()
  },
  methods: {
    dateChange() {
      const year = this.currentYear
      const month = this.currentMonth
      const date = this.currentDate1
      this.currentDate = new Date(`${year}-${month}-${date}`)
    },
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-calendar-day {
  height: auto;
  padding: 0;
}
::v-deep .el-calendar-table__row td,
::v-deep .el-calendar-table tr td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  // display: flex;
  // justify-content: center;
  // align-content: center;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
::v-deep .el-calendar__header {
  display: none;
}
</style>
